package com.fdb.efp.nls.service.impl;

import com.fdb.basic.framework.core.base.FrameworkService;
import com.fdb.basic.framework.core.util.DateTool;
import com.fdb.basic.framework.core.util.DateUtility;
import com.fdb.efp.nls.service.facade.LoanRepayDetailBatchService;
import com.fdb.efp.nls.common.LoanConstant;
import com.fdb.efp.nls.service.facade.*;
import com.fdb.efp.nls.service.vo.AccLoanVO;
import com.fdb.efp.nls.service.vo.GzBankRepayDetailFileTempVO;
import com.fdb.efp.loan.service.vo.LoanRepayDetailVO;
import com.fdb.efp.nls.service.vo.RepayDetailAcountTempVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * ServiceImpl: 还款明细表
 * <p>
 * 
 * @author cyj
 * @date 2018-12-27
 */
@Service("loanRepayDetailBatchService")
public class LoanRepayDetailBatchServiceImpl implements LoanRepayDetailBatchService, FrameworkService {

	private static Logger logger = LoggerFactory.getLogger(LoanRepayDetailBatchServiceImpl.class);

	@Autowired
	@Qualifier("loanRepayDetailService")
	private LoanRepayDetailService loanRepayDetailService;
	
	@Autowired
	@Qualifier("repayDetailAcountTempService")//////
	private RepayDetailAcountTempService repayDetailAcountTempService;
	
	@Autowired
	@Qualifier("gzBankRepayDetailFileTempService")/////////
	private GzBankRepayDetailFileTempService gzBankRepayDetailFileTempService;
	
	@Autowired
	@Qualifier("accoutErroTempService")
	private AccoutErroTempService accoutErroTempService;
	
	@Autowired
	@Qualifier("accLoanService")
	private AccLoanService accLoanService;

	/**
	 * 
	 * 同步马上的数据 新增 贷款还款明细 <br>
	 * 0.2:ludayong:2019年3月6日 下午5:10:56:修复贷款还款明细临时信息为空时，出现空指针异常<br>
	 * @author cenyj
	 * @since 2018年12月28日 下午3:33:37
	 * @date 2019年3月6日 下午5:10:56
	 * @version 0.2
	 */
	public boolean batchtLoanRepayDetailFromMaTxt() throws Exception {
		boolean rs = false;
		try {
			// 分页大小
			Integer pageSize = 1000;
			// 分页信息
			GzBankRepayDetailFileTempVO pageInfo = new GzBankRepayDetailFileTempVO();
			// 页数
			int page = 1;
			// pageSize初始化
			if (pageSize > 0) {
				pageInfo.setSize(pageSize);
			} else {
				pageInfo.setSize(1000);
			}
			// 判断循环次数
			int size = gzBankRepayDetailFileTempService.queryCount();
			String size_msg = "【还款明细信息表临时表】数据量：" + size;
			logger.info(size_msg, "message{}");
			int loopCount = (size / pageInfo.getSize()) + 1;

			// 对账数据
			BigDecimal principalDueTotal = BigDecimal.ZERO;
			BigDecimal principalAmtTotal = BigDecimal.ZERO;
			BigDecimal interestDueTotal = BigDecimal.ZERO;
			BigDecimal interestAmtTotal = BigDecimal.ZERO;
			BigDecimal penaltyDueTotal = BigDecimal.ZERO;
			BigDecimal penaltyAmtTotal = BigDecimal.ZERO;
			for (int i = 0; i < loopCount; i++) {
				String page_msg = "【还款明细信息表临时表】当前页数：" + pageInfo.getPage() + "，分页大小：" + pageInfo.getSize();
				logger.info(page_msg, "message{}");
				// 分页查询【还款明细信息表临时表】数据
				List<GzBankRepayDetailFileTempVO> tempList = gzBankRepayDetailFileTempService.queryByPage(pageInfo);
				if (Objects.nonNull(tempList) && !tempList.isEmpty()) {
					// 分页同步还款明细信息
					syncLoanRepayDetailInfo(tempList);
					// 对账数据
					principalDueTotal = principalDueTotal.add(tempList.stream().map(GzBankRepayDetailFileTempVO::getPrincipalDue).reduce(BigDecimal.ZERO, BigDecimal::add));
					principalAmtTotal = principalAmtTotal.add(tempList.stream().map(GzBankRepayDetailFileTempVO::getPrincipalAmt).reduce(BigDecimal.ZERO, BigDecimal::add));
					interestDueTotal = interestDueTotal.add(tempList.stream().map(GzBankRepayDetailFileTempVO::getInterestDue).reduce(BigDecimal.ZERO, BigDecimal::add));
					interestAmtTotal = interestAmtTotal.add(tempList.stream().map(GzBankRepayDetailFileTempVO::getInterestAmt).reduce(BigDecimal.ZERO, BigDecimal::add));
					penaltyDueTotal = penaltyDueTotal.add(tempList.stream().map(GzBankRepayDetailFileTempVO::getPenaltyDue).reduce(BigDecimal.ZERO, BigDecimal::add));
					penaltyAmtTotal = penaltyAmtTotal.add(tempList.stream().map(GzBankRepayDetailFileTempVO::getPenaltyAmt).reduce(BigDecimal.ZERO, BigDecimal::add));
				}
				pageInfo.setPage(++page);
			}

			// 先 对账表，看是否能对应上
			//logger.info("日终批量修改从马上同步过来的临时表数据到还款明细表：");
			// 先校对 对账临时表的记录条数跟同步过来的数据是否一致 如果一致 再批量 把临时表的数据同步到本地
			// 临时表
			
//			List<GzBankRepayDetailFileTempVO> list = gzBankRepayDetailFileTempService.getGzBankRepayDetailFileTempList();
//			//同步信息为空，返回false
//			if(Objects.isNull(list)) {
//				logger.info("日终批量-同步马上的贷款还款明细信息为空！");
//				return rs;
//			}
			
//			logger.info("马上同步过来的临时表数据：List<GzBankRepayDetailFileTempVO> list= gzBankRepayDetailFileTempService.getGzBankRepayDetailFileTempList()，记录条数："+list.size());
			// 对账列表
			List<RepayDetailAcountTempVO> accountList = repayDetailAcountTempService.getRepayDetailAcountTempList();
			// 对账临时表
			RepayDetailAcountTempVO accountTempModel = null;//new RepayDetailAcountTempVO();
			logger.info("还款明细对账开始.....");
			// 临时表列表的sum
//			BigDecimal principalDueTotal = list.stream().map(GzBankRepayDetailFileTempVO::getPrincipalDue).reduce(BigDecimal.ZERO, BigDecimal::add);
//			BigDecimal principalAmtTotal = list.stream().map(GzBankRepayDetailFileTempVO::getPrincipalAmt).reduce(BigDecimal.ZERO, BigDecimal::add);
//			BigDecimal interestDueTotal = list.stream().map(GzBankRepayDetailFileTempVO::getInterestDue).reduce(BigDecimal.ZERO, BigDecimal::add);
//			BigDecimal interestAmtTotal = list.stream().map(GzBankRepayDetailFileTempVO::getInterestAmt).reduce(BigDecimal.ZERO, BigDecimal::add);
//			BigDecimal penaltyDueTotal = list.stream().map(GzBankRepayDetailFileTempVO::getPenaltyDue).reduce(BigDecimal.ZERO, BigDecimal::add);
//			BigDecimal penaltyAmtTotal = list.stream().map(GzBankRepayDetailFileTempVO::getPenaltyAmt).reduce(BigDecimal.ZERO, BigDecimal::add);

		BigDecimal principalDueTotalSum =  principalDueTotal == null ? BigDecimal.ZERO : principalDueTotal ;
		BigDecimal principalAmtTotalSum =  principalAmtTotal == null ? BigDecimal.ZERO : principalAmtTotal ;
		BigDecimal interestDueTotalSum =  interestDueTotal == null ? BigDecimal.ZERO : interestDueTotal ;
		BigDecimal interestAmtTotalSum =  interestAmtTotal == null ? BigDecimal.ZERO : interestAmtTotal ;
		BigDecimal penaltyDueTotalSum =  penaltyDueTotal == null ? BigDecimal.ZERO : penaltyDueTotal ;
		BigDecimal penaltyAmtTotalSum =  penaltyAmtTotal == null ? BigDecimal.ZERO : penaltyAmtTotal ;

		int accountCountTemp = 0;// 对账表记录的条数
		int loanTempDatasize = 0;// 实际同步过来的数据总条数
		if (accountList != null && accountList.size() > 0) {
			accountTempModel = accountList.get(0);
		}
		if (accountTempModel != null) {
			accountCountTemp = accountTempModel.getDataAcount() == null ? 0 : accountTempModel.getDataAcount();
			String tableName = "GzBankRepayDetailFileTemp";
			if (accountTempModel.getPrincipalDueTotal().compareTo( principalDueTotalSum) !=0) {
				String erroMsg = "应还本金总额getPrincipalDueTotal不对";
				String  acountTotal= "" + accountTempModel.getPrincipalDueTotal();
				String  tempTotal= "" + principalDueTotalSum;
				//String type, String updateDelInster,String no, String eMsg
				accoutErroTempService.accountErroMsg(tableName, erroMsg, tempTotal, acountTotal,"1","","","");
			}
			if (accountTempModel.getPrincipalAmtTotal().compareTo( principalAmtTotalSum) !=0) {
				String erroMsg = "还款本金总额getPrincipalAmtTotal不对";
				String  acountTotal= "" + accountTempModel.getPrincipalAmtTotal();
				String  tempTotal= "" + principalAmtTotalSum;
				accoutErroTempService.accountErroMsg(tableName, erroMsg, tempTotal, acountTotal,"1","","","");
			}
			if (accountTempModel.getInterestDueTotal().compareTo( interestDueTotalSum) !=0) {
				String erroMsg = "应还利息总额getInterestDueTotal不对";
				String  acountTotal= "" + accountTempModel.getInterestDueTotal();
				String  tempTotal= "" + interestDueTotalSum;
				accoutErroTempService.accountErroMsg(tableName, erroMsg, tempTotal, acountTotal,"1","","","");
			}
			if (accountTempModel.getInterestAmtTotal().compareTo( interestAmtTotalSum) !=0) {
				String erroMsg = "还款利息总额getInterestAmtTotal不对";
				String  acountTotal= "" + accountTempModel.getInterestAmtTotal();
				String  tempTotal= "" + interestAmtTotalSum;
				accoutErroTempService.accountErroMsg(tableName, erroMsg, tempTotal, acountTotal,"1","","","");
			}
			if (accountTempModel.getPenaltyDueTotal().compareTo( penaltyDueTotalSum) !=0) {
				String erroMsg = "应还罚息总额getPrincipalDueTotal不对";
				String  acountTotal= "" + accountTempModel.getPenaltyDueTotal();
				String  tempTotal= "" + penaltyDueTotalSum;
				accoutErroTempService.accountErroMsg(tableName, erroMsg, tempTotal, acountTotal,"1","","","");
			}
			//还款罚息总额getPrincipalDueTotal不对
			if (accountTempModel.getPenaltyAmtTotal().compareTo( penaltyAmtTotalSum) !=0) {//getPenaltyAmt
				String erroMsg = "还款罚息总额getPenaltyAmtTotal不对";
				String  acountTotal= "" + accountTempModel.getPenaltyAmtTotal();
				String  tempTotal= "" + penaltyAmtTotalSum;
				accoutErroTempService.accountErroMsg(tableName, erroMsg, tempTotal, acountTotal,"1","","","");
			}
		}
//		loanTempDatasize = list.size();
		loanTempDatasize = size;

		logger.info("对账表记录的条数loanTempDatasize：" + loanTempDatasize + "实际同步过来的数据总条数loanTempDatasize" + loanTempDatasize);
		if (loanTempDatasize != accountCountTemp) {
			// 如果条数不一样，则记录到erro_msg表
			String erroMsg = "记录条数不对";
			String tableName = "GzBankRepayDetailFileTemp";
			String tempTotal = "" + loanTempDatasize;
			String acountTotal = "" + accountCountTemp;
			accoutErroTempService.accountErroMsg(tableName, erroMsg, tempTotal, acountTotal,"1","","","");

		}
		logger.info("还款明细对账结束.....");
		rs = true;
	} catch (Exception e) {
		logger.error("还款明细数据信息出错!",e);
		e.printStackTrace();
		String tableName="LoanRepayDetail还款明细表";
		String erroMsg="";
		String tempTotal="";
		String acountTotal="";
		String type="0";
		String updateDelInster="";
		String no="";
		String eMsg=e.getMessage();							
		accoutErroTempService.accountErroMsg(tableName, erroMsg, tempTotal, acountTotal,type,updateDelInster,no,eMsg);

		rs = false;
	}
	logger.info("整个还款明细数据方法结束，rs："+rs );
	return rs;

}

/**
 * 还款明细新增
 * 
 * @description:TODO
 * @param inLoanRepayDetailVo
 * @return
 * @date:2018年12月28日 下午3:24:47
 * @author:cenyj
 */
public int insertLoanRepayDetail(LoanRepayDetailVO inLoanRepayDetail) {
	logger.debug("当前新增数据为:" + inLoanRepayDetail.toString());
	int num = 0;
	try {
		num = loanRepayDetailService.insertLoanRepayDetail(inLoanRepayDetail);
	} catch (Exception e) {
		logger.error("新增数据发生异常!", e);
		num = -1;
	}
	logger.debug("当前新增数据条数为:" + num);
	return num;
}

/**
 * 删除操作
 */
public int deleteByPk(LoanRepayDetailVO inLoanRepayDetailVo) {
	logger.debug("当前删除数据条件为:" + inLoanRepayDetailVo);
	int num = 0;
	try {
		
		num = loanRepayDetailService.deleteByPk(inLoanRepayDetailVo);
	} catch (Exception e) {
		logger.error("删除数据发生异常!", e);
		num = -1;
	}
	logger.debug("根据条件:" + inLoanRepayDetailVo + "删除的数据条数为" + num);
	return num;
}

/**
 * 更新操作
 */
public int updateByPk(LoanRepayDetailVO inLoanRepayDetailVo) {
	logger.debug("当前修改数据为:" + inLoanRepayDetailVo.toString());
	int num = 0;
	try {
		
		num = loanRepayDetailService.updateByPk(inLoanRepayDetailVo);
	} catch (Exception e) {
		logger.error("修改数据发生异常!", e);
		num = -1;
	}
	logger.debug("根据条件:" + inLoanRepayDetailVo + "修改的数据条数为" + num);
	return num;
}

/**
 * 查询操作
 */
public LoanRepayDetailVO queryByPk(LoanRepayDetailVO inLoanRepayDetailVo) {

	logger.debug("当前查询参数信息为:" + inLoanRepayDetailVo);
	try {
	
		LoanRepayDetailVO queryRslLoanRepayDetail = loanRepayDetailService.queryByPk(inLoanRepayDetailVo);
		if (Objects.nonNull(queryRslLoanRepayDetail)) {
			LoanRepayDetailVO outLoanRepayDetailVo = beanCopy(queryRslLoanRepayDetail, new LoanRepayDetailVO());
			logger.debug("当前查询结果为:" + outLoanRepayDetailVo.toString());
			return outLoanRepayDetailVo;
		} else {
			logger.debug("当前查询结果为空!");
		}
	} catch (Exception e) {
		logger.error("查询数据发生异常!", e);
	}
	return null;
}

	/**
	 *需求编号：【2019D1204】（马上账务文件同步分页处理）
	 *问题编号：【2019D1204】（马上账务文件同步分页处理）
	 *开发人员：【ludayong】
	 *创建日期：【2019/11/27 11:31】
	 *功能描述：分页同步还款明细信息
	 *修改日期：【2019/11/27 11:31】
	 *修改描述：
	 */
	public void syncLoanRepayDetailInfo(List<GzBankRepayDetailFileTempVO> list) {
		try {
			int num = 0;
			// 本地表进行修改INSERT
			AccLoanVO acc_loan = new AccLoanVO();
			if (list != null && list.size() > 0) {
				logger.info("得到还款明细的临时数据");
				for (GzBankRepayDetailFileTempVO fileTemp : list) {
					logger.info("开始循环还款明细的临时数据");
					// 因爲有可能出现我们审批没有通过，但马上放款的数据，所以先判断本地借据表是否有对应的借据号 有再新增
					acc_loan.setBillNo(fileTemp.getRefNbr());
					AccLoanVO accLoan = accLoanService.queryByPk(acc_loan);
					if (accLoan != null) {
						logger.info("找到了对应的借据信息，开始给还款明细赋值");
						LoanRepayDetailVO inLoanRepayDetail = new LoanRepayDetailVO();
						inLoanRepayDetail.setSetlSeq(fileTemp.getRepayRefNbr());
						try {
							inLoanRepayDetail.setLoanNo(fileTemp.getRefNbr());
							// repay_type 还款类型 "01、按期还款 02、提前还款 03、逾期还款 04、非应计还款"  05.理赔还款？？？我库没有？
							/*
							 * 01、按期还款 02、提前还款
							 */
						/*01：正常还款
						02：提前部分还款
						03：提前结清
						04：逾期还款
						05：理赔还款*/
							String repayTypeMy = "";
							switch (fileTemp.getRepayType()) {
								case "01"://01、按期还款
									repayTypeMy = "01";//01正常还款
									break;
								case "02"://02、提前还款
									repayTypeMy = "03";//03：提前结清
									break;
								case "03":// 03、逾期还款
									repayTypeMy = "04";//04：逾期还款
									break;
								default:
									break;
							}
							inLoanRepayDetail.setSetlTyp(repayTypeMy);// 取字典库数据
							inLoanRepayDetail.setAllOdIntAmt(fileTemp.getPenaltyDue() == null ? BigDecimal.ZERO : fileTemp.getPenaltyDue());

							inLoanRepayDetail.setAllOdNormIntAmt(fileTemp.getInterestDue() == null ? BigDecimal.ZERO : fileTemp.getInterestDue());
							inLoanRepayDetail.setAllOdPrcpAmt(fileTemp.getPrincipalDue() == null ? BigDecimal.ZERO : fileTemp.getPrincipalDue());
							//本次还款总计金额 penalty_amt+interest_amt+principal_amt+total_fee
							BigDecimal sumAmt = fileTemp.getPenaltyAmt()== null ? BigDecimal.ZERO : fileTemp.getPenaltyAmt()
									.add(
											fileTemp.getInterestAmt()== null ? BigDecimal.ZERO : fileTemp.getInterestAmt()
													.add( fileTemp.getPrincipalAmt()== null ? BigDecimal.ZERO : fileTemp.getPrincipalAmt())
											//.add( inLoanRepayDetail.getTotalFee()== null ? BigDecimal.ZERO : inLoanRepayDetail.getTotalFee())
									) ;
							inLoanRepayDetail.setTotalAmt( sumAmt);

							inLoanRepayDetail
									.setSetlOdIntAmt( fileTemp.getPenaltyAmt() == null ? BigDecimal.ZERO : fileTemp.getPenaltyAmt());

							inLoanRepayDetail.setSetlOdNormInt(
									fileTemp.getInterestAmt() == null ? BigDecimal.ZERO : fileTemp.getInterestAmt());

							inLoanRepayDetail
									.setSetlOdPrcpAmt( fileTemp.getPrincipalAmt() == null ? BigDecimal.ZERO : fileTemp.getPrincipalAmt());
							SimpleDateFormat sdfTime = new SimpleDateFormat("yyyy-MM-dd");//

							String repayDate=DateUtility.format8To10(fileTemp.getRepayDate());	//还款日期
							inLoanRepayDetail.setSetlApplyDt(repayDate);//主动还款申请日期
							SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 日期要什么格式？
							String currDate = sdf.format(new Date());
							inLoanRepayDetail.setSetlCreateDt(currDate);//
							inLoanRepayDetail.setSetlValDt(repayDate);//生效日期
							//repay_card_no	disb_ac_no	还款银行卡号
							inLoanRepayDetail.setDisbAcNo(fileTemp.getRepayCardNo());
							//20190215 modify cyj

							//setl_rem_prcp_paym 提前还本金额 条件输入  setl_od_prcp_amt	归还欠本金额  //principal_amt
							if(fileTemp.getRepayType().equals("02"))//02、提前还款
							{
								inLoanRepayDetail.setSetlRemPrcpPaym(fileTemp.getPrincipalAmt() == null ? BigDecimal.ZERO : fileTemp.getPrincipalAmt());
							}
							// 放款金额 赋值 setdnamt
							inLoanRepayDetail.setDnAmt(accLoan.getLoanAmount() == null ? BigDecimal.ZERO : accLoan.getLoanAmount());
							// int_rat 执行年利率 赋值
							inLoanRepayDetail.setIntRat(accLoan.getRealityIrY() == null ? BigDecimal.ZERO : accLoan.getRealityIrY());
							inLoanRepayDetail.setRepayTerm(fileTemp.getRepayTerm().toString());

							String currentTime = DateTool.getCurrentDateTime();
							String createUser = "admin";
							// last_chg_usr 最新变更用户 admin admin
							inLoanRepayDetail.setLastChgUsr(createUser);
							// last_chg_dt 最新变更时间 new date new date
							inLoanRepayDetail.setLastChgDt(currentTime);

							logger.info("找到了对应的借据信息，结束给还款明细赋值");
							logger.info("新增还款明细开始");

							//查询还款明细信息是否存在，存在则更新，不存在则新增
							LoanRepayDetailVO queryLoanRepayDetailVO = loanRepayDetailService.queryLoanRepayDetailByCondition(inLoanRepayDetail);
							if(Objects.nonNull(queryLoanRepayDetailVO)) {
								//更新还款明细信息
								num = loanRepayDetailService.updateLoanRepayDetailByCondition(inLoanRepayDetail);
							}else {
								//新增还款明细信息
								num = insertLoanRepayDetail(inLoanRepayDetail);
							}

							logger.info("新增还款明细结束，返回值：num"+num);
							if(num == -1) {
								logger.info("新增还款明细失败");
								logger.error("新增数据信息出错!");
								String tableName="LoanRepayDetail还款明细表";
								String erroMsg="";
								String tempTotal="";
								String acountTotal="";
								String type="0";
								String updateDelInster="新增";
								String no=fileTemp.getRefNbr();
								String eMsg="新增数据信息出错!";
								accoutErroTempService.accountErroMsg(tableName, erroMsg, tempTotal, acountTotal,type,updateDelInster,no,eMsg);
							}else {
								//// 查询还款明细
								logger.info("新增还款明细成功");
								LoanRepayDetailVO loanRepayDetailVO=new LoanRepayDetailVO();
								loanRepayDetailVO.setLoanNo(accLoan.getBillNo());
								List<LoanRepayDetailVO>  loanRepayDetailList=
										loanRepayDetailService.queryAllByLoanNo(loanRepayDetailVO);
								// latest_repay_date 最近还款日期 赋值（取还款明细的还款日期？）//getLatestRepayDate
								logger.info("取借据号为："+accLoan.getBillNo()+"的最近还款日期");
								if (loanRepayDetailList != null) {
									List<LocalDate> list2 = loanRepayDetailList.stream().map(LoanRepayDetailVO::getSetlApplyDt)
											.map(o -> LocalDate.parse(o, DateTimeFormatter.ISO_DATE))
											.sorted().collect(Collectors.toList());
									if(list2 != null && list.size()>0) {
										String lastTime = list2.get(list2.size() - 1).format(DateTimeFormatter.ISO_DATE);
										accLoan.setLatestRepayDate(lastTime);
										//台账状态：结清、理赔结清
										if (LoanConstant.TZZT_JQ.equals(accLoan.getAccountStatus())) {
											// // settl_date 结清日期 赋值  结清时： 业务日期
											accLoan.setSettlDate(lastTime);
											logger.info("借据表最近还款日期赋值为："+lastTime+"");
										}
										logger.info("给借据表的最近还款日期赋值为："+lastTime+"");
									}
									// repayment_account 还款账号最新的 赋值？还款明细 的账号  repay_card_no
									////repay_card_no	disb_ac_no	还款银行卡号setDisbAcNo  getRepayCardNo
									List<String> list3 = loanRepayDetailList.stream().map(LoanRepayDetailVO::getDisbAcNo)
											.collect(Collectors.toList());
									if(list3 != null && list.size()>0) {
										String repaymentAccount = list3.get(list3.size() - 1);
										accLoan.setRepaymentAccount(repaymentAccount);
										logger.info("给借据表的还款账号赋值为："+repaymentAccount+"");
									}
								}
								logger.info("开始更新借据表");
								num = accLoanService.updateByPk(accLoan);// 更新
								logger.info("结束更新借据表，结果返回值为num:"+num);
								if (num == -1) {
									logger.info("更新借据表失败！");
									logger.error("更新数据发生异常");
									String tableName = "AccLoan贷款台账表";
									String erroMsg = "";
									String tempTotal = "";
									String acountTotal = "";
									String type = "0";
									String updateDelInster = "更新";
									String no = fileTemp.getRefNbr();
									String eMsg = "";
									accoutErroTempService.accountErroMsg(tableName, erroMsg, tempTotal, acountTotal, type,
											updateDelInster, no, eMsg);

								}
							}
						} catch (Exception e) {
							logger.info("新增还款明细出现异常！");
							logger.error("新增数据信息出错!");
							e.printStackTrace();
							String tableName="LoanRepayDetail还款明细表";
							String erroMsg="";
							String tempTotal="";
							String acountTotal="";
							String type="0";
							String updateDelInster="新增";
							String no=fileTemp.getRefNbr();
							String eMsg=e.getMessage();
							accoutErroTempService.accountErroMsg(tableName, erroMsg, tempTotal, acountTotal,type,updateDelInster,no,eMsg);
						}
					} else {
						logger.info("没有对应的借据数据信息!");// 跟审批表对应不上，比如：没有审批通过，马上也放款的情况
						String tableName="LoanRepayDetail还款明细表";
						String erroMsg="";
						String tempTotal="";
						String acountTotal="";
						String type="0";
						String updateDelInster="新增";
						String no=fileTemp.getRefNbr();
						String eMsg="没有对应的借据数据信息!";
						accoutErroTempService.accountErroMsg(tableName, erroMsg, tempTotal, acountTotal,type,updateDelInster,no,eMsg);
					}
				}
			}
		} catch (Exception e) {
			logger.error("同步马上还款明细信息数据发生异常!", e);
		}
	}
}
